package com.wx.app.ui.activity;

import android.animation.ValueAnimator;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.ViewTreeObserver;
import android.widget.Button;
import android.widget.TextView;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.bigkoo.convenientbanner.ConvenientBanner;
import com.bigkoo.convenientbanner.holder.CBViewHolderCreator;
import com.example.administrator.newemptyapplication.R;
import com.google.zxing.integration.android.IntentIntegrator;
import com.google.zxing.integration.android.IntentResult;
import com.wx.app.mylib.activity.CityListActivity;
import com.wx.app.mylib.app.BaseActivity;
import com.wx.app.mylib.app.Globals;
import com.wx.app.ui.utils.NetworkImageHolderView;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import de.hdodenhof.circleimageview.CircleImageView;
import okhttp3.Call;

public class Test2 extends BaseActivity {

    @BindView(R.id.AVP_one)
    ConvenientBanner avpOne;
    @BindView(R.id.btn_clear_cache)
    Button btnClear;
    @BindView(R.id.btn_video)
    Button btnVideo;
    @BindView(R.id.iv_circle)
    CircleImageView ivHeader;
    @BindView(R.id.btn_city)
    Button btnCity;
    @BindView(R.id.tv_test2_city)
    TextView tvCity;

    ArrayList<String> bannerData = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test2);
        ButterKnife.bind(this);
        EventBus.getDefault().register(mActivity);
        initHeader();
        initBanner();
        initData();
    }

    private void initHeader() {
        ivHeader.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                ivHeader.getViewTreeObserver().removeGlobalOnLayoutListener(this);
                ivHeader.setPivotY(0f);
                ivHeader.setPivotX(ivHeader.getWidth() / 2);
                ValueAnimator roateX = ValueAnimator.ofFloat(-1f, 1f);
                roateX.setRepeatCount(-1);
                roateX.setRepeatMode(ValueAnimator.REVERSE);
                roateX.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                    @Override
                    public void onAnimationUpdate(ValueAnimator animation) {
                        float currentValue = (Float) animation.getAnimatedValue();
                        ivHeader.setRotationX(currentValue * 30f);
                    }
                });
                roateX.setDuration(2000).start();
            }
        });
    }

    @OnClick(R.id.btn_city)void clickCity(){
        Intent intent = new Intent(mActivity, CityListActivity.class);
        startActivity(intent);
    }

    @OnClick(R.id.btn_clear_cache)
    void clear() {
        showToast("二维码！");
        Globals.ZxingScan(mActivity);
    }

    @OnClick(R.id.btn_video)
    void playVideo() {
        Intent intent = new Intent(mActivity, VideoPlayActivity.class);
        startActivity(intent);
    }

    private void initData() {
        String url = "http://7xl2ga.com1.z0.glb.clouddn.com/Banners.txt";
        OkHttpUtils.get()
                .url(url)
                .build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Call call, Exception e) {
                        showToast("请求服务器出错！");
                    }

                    @Override
                    public void onResponse(String response) {
                        JSONObject data = JSONObject.parseObject(response);
                        JSONArray array = data.getJSONArray("bannerData");
                        for (int i = 0; i < array.size(); i++) {
                            JSONObject object = array.getJSONObject(i);
                            String url = object.getString("banner_url");
                            bannerData.add(url);
                        }
                        avpOne.notifyDataSetChanged();
                    }
                });
    }

    private void initBanner() {
        avpOne.setPages(new CBViewHolderCreator<NetworkImageHolderView>() {
            @Override
            public NetworkImageHolderView createHolder() {
                return new NetworkImageHolderView();
            }
        }, bannerData)
                .setPageIndicator(new int[]{R.drawable.ic_page_indicator, R.drawable.ic_page_indicator_focused});
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        IntentResult result = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
        if (result != null) {
            if (result.getContents() == null) {
                Log.d("Test2", "取消扫描");
                showToast("取消扫描！");
            } else {
                Log.d("Test2", "扫描成功");
                showToast("扫描成功！");
            }
        } else {
            // This is important, otherwise the result will not be passed to the fragment
            super.onActivityResult(requestCode, resultCode, data);
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void hell(String str) {
        tvCity.setText(str);
    }

    @Override
    protected void onResume() {
        super.onResume();
        avpOne.startTurning(2000);
    }

    @Override
    protected void onPause() {
        super.onPause();
        avpOne.stopTurning();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}
